import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;

// Project: 

/******************************************************************************
 * <p>
 * File: GamePanel.java
 * </p>
 * @author  Tadhg O'Sullivan, rogue
 * @author <a href="mailto:tadhg.osullivan@ucdconnect.ie">tadhg.osullivan@ucdconnect.ie</a>
 * @version 1.0, 2006
 * @created Dec 22, 2006 3:47:51 PM
 * @revised 
 * <p>
 * Description:
 * </p>
 *****************************************************************************/

public class GamePanel extends JPanel implements Runnable{
	
	private static final int PWIDTH = 500; // size of panel
	private static final int PHEIGHT = 400;
	
	private Thread animator; // for the animation loop
	private volatile boolean running = false; // stops the animation
	
	private volatile boolean gameOver = false; // for game termination
	
	// more variables explained later
	// :
	
	public GamePanel(){
		
		setBackground(Color.white); // white background
		setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
		
		// create game components
		// ...
	} // end of GamePanel

	/**
	 * Wait for the JPanel to be added to the JFrame/JApplet befor starting
	 */
	public void addNotify(){
		super.addNotify(); // creates the peer
		startGame(); // start the thread
	}
	
	// initialise and start the thread
	private void startGame(){
		if(animator == null || !running){
			animator = new Thread(this);
			animator.start();
		}
	} // end of startGame
	
	// called by the user to stop execution
	public void stopGame(){
		running = false;
	}
	
	/**
	 * repeatedly update, render, sleep
	 */
	public void run(){
		running = true;
		while(running){
			gameUpdate(); // game state is updated
			gameRender(); // render to a buffer
			repaint(); // paint with buffer
			
			try{
				Thread.sleep(20); // sleep a bit
			}catch(InterruptedException ex){}
		}
		System.exit(0); // so enclosing JFrame/JApplet exits
	} //end of run
	
	private void gameUpdate(){
		if(!gameOver){
			// update game state
		}
	}
	
	// more methods explained later
	
}// end of GamePanel class

